package com.wwfish.cmsui.modules.resource.client;

import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.*;
import com.nexustar.gwt.widgets.client.asyn.FishAsyncCallback;
import com.nexustar.gwt.widgets.client.ui.HasModel;
import com.nexustar.gwt.widgets.client.ui.ModelManager;
import com.nexustar.gwt.widgets.client.ui.button.MyButton;
import com.nexustar.gwt.widgets.client.ui.form.*;
import com.nexustar.gwt.widgets.client.ui.panel.InfoPanel;
import com.nexustar.gwt.widgets.client.ui.textbox.*;
import com.nexustar.gwt.widgets.client.ui.window.PopPanel;
import com.wwfish.cms.model.CMSConstants;
import com.wwfish.cms.model.ResourceDto;
import com.wwfish.cms.service.CodeManager;
import com.wwfish.cms.service.ResourceManager;
import com.wwfish.cmsui.dashboard.client.CMSDashboard;
import com.wwfish.cmsui.modules.common.client.util.CodesHelper;
import com.wwfish.cmsui.modules.common.client.util.ServiceFactory;
import com.wwfish.gwt.reflection.client.ModelReflection;

/**
 * Created by IntelliJ IDEA.
 * User: Tom
 * Date: 2010-8-9
 * Time: 10:32:16
 * To change this template use File | Settings | File Templates.
 */
public class ResourceWidget implements HasModel {

    private PopPanel panel;
    private FormContainer contentForm;
    private ModelManager mm = new ModelManager(this);
    private FishLabelImageBox image;
    private FishLabelUploadBox file;
    private MyButton save;

    private static final String _CODE_IMAGE_TYPE = "0";

    public ResourceWidget() {
        panel = new PopPanel();
        initForm();
        VerticalPanel vp = new VerticalPanel();
        vp.add(contentForm);
        Widget bar = getButtonBar();
        vp.add(bar);
        vp.setCellHorizontalAlignment(bar, HasHorizontalAlignment.ALIGN_CENTER);
        panel.addWidget(vp);

    }

    public void show(ResourceDto dto) {
        getModelManger().renderModel(dto);
        panel.center();
    }

    private Widget getButtonBar() {
        HorizontalPanel bar = new HorizontalPanel();
        save = new MyButton("确认");
        MyButton cancel = new MyButton("取消");
        HTML n = new HTML("&nbsp;&nbsp;&nbsp;&nbsp;");
        bar.add(save);
        bar.add(n);
        bar.add(cancel);
        save.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent clickEvent) {
                if (!contentForm.isValid()) return;
                ResourceDto dto = (ResourceDto) contentForm.getModelManger().getModel();
                ServiceFactory.invoke(ResourceManager.class.getName(), "saveAndUpdate", new Object[]{dto}, new FishAsyncCallback() {
                    public void onSuccess(Object o) {
                        panel.hide();
                        InfoPanel.show("资源保存成功！");
                    }
                });
            }
        });
        cancel.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent clickEvent) {
                panel.hide();
            }
        });

        return bar;
    }

    private void changeType(String value) {
        if (value.equals(_CODE_IMAGE_TYPE)) {
            image.setVisible(true);
            file.setVisible(false);
        } else {
            image.setVisible(false);
            file.setVisible(true);
        }
    }

    private void initForm() {
        contentForm = new FormContainer(1);
        final FishLabelTextBox name = new FishLabelTextBox("资源名称:");
        final FishLabelListBox type = new FishLabelListBox("类型:");
        final FishLabelTextBox keywords = new FishLabelTextBox("关键字:");
        final FishLabelTextAreaBox description = new FishLabelTextAreaBox("资源描述:");
        image = new FishLabelImageBox("图片:");
        file = new FishLabelUploadBox("资源文件:");

        ((ListBox) type.getUI()).addChangeHandler(new ChangeHandler() {
            public void onChange(ChangeEvent changeEvent) {
                changeType((String) type.getValue());

            }
        });

        CodesHelper.initGeneralCodesToListWidget(CodesHelper.getCodesById(CMSConstants._CODE_RESOURCE_VARIETY_ID), (ListBox) type.getUI(), false);

        contentForm.addElement(new FormElement(type, new FormElementProviderAdpter() {
            public void setValue(Object object) {
                ResourceDto dto = (ResourceDto) object;
                type.setValue(dto.getType());
                changeType(dto.getType());
            }

            public void getValue(Object object) {
                ResourceDto dto = (ResourceDto) object;
                dto.setType((String) type.getValue());
            }

        }));
        contentForm.addElement(new FormElement(name, new FormElementProviderAdpter() {
            public void setValue(Object object) {
                ResourceDto dto = (ResourceDto) object;
                name.setValue(dto.getName());
            }

            public void getValue(Object object) {
                ResourceDto dto = (ResourceDto) object;
                dto.setName((String) name.getValue());
            }

        }, new IValidator[]{ValidatorCreator.require()}));
        contentForm.addElement(new FormElement(keywords, new FormElementProviderAdpter() {
            public void setValue(Object object) {
                ResourceDto dto = (ResourceDto) object;
                keywords.setValue(dto.getKeyWord());
            }

            public void getValue(Object object) {
                ResourceDto dto = (ResourceDto) object;
                dto.setKeyWord((String) keywords.getValue());
            }

        }));
        contentForm.addElement(new FormElement(description, new FormElementProviderAdpter() {
            public void setValue(Object object) {
                ResourceDto dto = (ResourceDto) object;
                description.setValue(dto.getDescription());
            }

            public void getValue(Object object) {
                ResourceDto dto = (ResourceDto) object;
                dto.setDescription((String) description.getValue());
            }

        }));
        contentForm.addElement(new FormElement(image, new FormElementProviderAdpter() {
            public void setValue(Object object) {
                ResourceDto dto = (ResourceDto) object;
                //todo:
                if (dto.getType().equals(_CODE_IMAGE_TYPE))
                    image.setValue(dto.getResourceName());
            }

            public void getValue(Object object) {
                ResourceDto dto = (ResourceDto) object;
                //todo:
                if (type.equals(_CODE_IMAGE_TYPE))
                    dto.setResourceName((String) image.getValue());
            }

        }));
        contentForm.addElement(new FormElement(file, new FormElementProviderAdpter() {
            public void setValue(Object object) {
                ResourceDto dto = (ResourceDto) object;
                //todo:
                if (!dto.getType().equals(_CODE_IMAGE_TYPE))
                    file.setValue(dto.getResourceName());
            }

            public void getValue(Object object) {
                ResourceDto dto = (ResourceDto) object;
                //todo:
                if (!type.equals(_CODE_IMAGE_TYPE))
                    dto.setResourceName((String) file.getValue());
            }

        }));
    }

    public void ddOutModel(Object[] parameters, Object model) {
        if (model == null) {
            model = getNewDto();
            getModelManger().cacheModel(model);
            panel.setWindowTitle("创建资源");
        } else
            panel.setWindowTitle("编辑资源");

        contentForm.getModelManger().renderModel(ModelReflection.clone(model));
    }

    public void ddIntModel(Object model) {
    }

    private ResourceDto getNewDto() {
        ResourceDto dto = new ResourceDto();
        dto.setType("0");
        dto.setAuthor(CMSDashboard.user.getId() + "");
        return dto;
    }


    public ModelManager getModelManger() {

        return mm;
    }
}
